package org.jobtask.ui;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.DefaultTableModel;

import org.jobtask.config.ResourceMessages;
import org.jobtask.iface.ControlListener;
import org.jobtask.schedule.JobTask;
import org.jobtask.schedule.KeyValue;

public class CommonComponents {

	private JFrame win=null;
	private JButton addTaskBut=null;
	private JButton delTaskBut=null;
	private JList taskList=null;
	private JList taskGroupList=null;
	private JTable infoTable=null;
	private JButton addGoupBut=null;
	private JButton delGoupBut=null;
	private JToggleButton ctrlBut=null;
	private JButton clearBut=null;
	private JTextPane text=null;
	private JTextField startCountLab=null;
	private JTextField stopCountLab=null;
	private JTextField totalCountLab=null;
//	private JCheckBox minInTrayCheckBut=null;
//	private JCheckBox winTopCheckBut=null;
	private JLabel infoLab=new JLabel(ResourceMessages.getString("CommonComp.19"));

	//开始菜单项
	private JMenuItem logsMenu=null;
	private JMenuItem modifyTaskMenu=null;
	private JMenuItem saveMenu=null;
	private JMenuItem reloadMenu=null;
	private JMenuItem minimiToTrayMenu=null;
	private JMenuItem winTopMenu=null;
	private JMenuItem helpMenu=null;
	private JMenuItem aboutMenu=null;
	private JMenuItem exitMenu=null;
	private JMenuItem nurseMenu=null;
	
	
	//任务列表右键菜单
	private JPopupMenu taskPopupMenu=null;
	private JMenuItem newMenu=null;
	private JMenuItem modifyMenu=null;
	private JMenuItem removeMenu=null;
	private JMenu moveToMenu=null;
	private JMenuItem selectAll=null;
	
	public JFrame getWin() {
		return win;
	}
	
	{
		win=new  JFrame(ResourceMessages.getString("CommonComp.0")); 
		FontUIResource fontRes = new FontUIResource(UIConst.FONT14);   
	    for(Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();)   
	    {   
	        Object key = keys.nextElement();   
	        Object value = UIManager.get(key);   
	        if(value instanceof FontUIResource)   
	            UIManager.put(key, fontRes);   
	    }   
		win.setIconImage(new ImageIcon(this.getClass().getResource("images/wintitle.png")).getImage()); 
	}
	
	public JButton getDelGoupBut() {
		return delGoupBut;
	}

	public JButton getAddGoupBut() {
		return addGoupBut;
	}

	public JList getTaskGroupList() {
		return taskGroupList;
	}

	public JTable getInfoTable() {
		return infoTable;
	}

	public JButton getAddTaskBut() {
		return addTaskBut;
	}
	public JButton getDelTaskBut() {
		return delTaskBut;
	}
	public JList getTaskList() {
		return taskList;
	}
	
	public JToggleButton getCtrlBut() {
		return ctrlBut;
	}

	public JTextPane getText() {
		return text;
	}

	public JButton getClearBut() {
		return clearBut;
	}
	

	public JTextField getStartCountLab() {
		return startCountLab;
	}

	public JTextField getStopCountLab() {
		return stopCountLab;
	}

	public JTextField getTotalCountLab() {
		return totalCountLab;
	}

	public CommonComponents(){
		this.init();
	}
	
	
	public JMenuItem getLogsMenu() {
		return logsMenu;
	}

	public JMenuItem getModifyTaskMenu() {
		return modifyTaskMenu;
	}

	public JMenuItem getSaveMenu() {
		return saveMenu;
	}

	public JMenuItem getReloadMenu() {
		return reloadMenu;
	}

	public JMenuItem getMinimiToTrayMenu() {
		return minimiToTrayMenu;
	}

	public JMenuItem getWinTopMenu() {
		return winTopMenu;
	}

	public JMenuItem getHelpMenu() {
		return helpMenu;
	}

	public JMenuItem getAboutMenu() {
		return aboutMenu;
	}

	public JMenuItem getExitMenu() {
		return exitMenu;
	}
	
	public JMenuItem getNurseMenu() {
		return nurseMenu;
	}
	

	public JPopupMenu getTaskPopupMenu() {
		return taskPopupMenu;
	}

	public JMenuItem getNewMenu() {
		return newMenu;
	}

	public JMenuItem getModifyMenu() {
		return modifyMenu;
	}

	public JMenuItem getRemoveMenu() {
		return removeMenu;
	}

	public JMenuItem getMoveToMenu() {
		return moveToMenu;
	}
	
	public JLabel getInfoLab() {
		return infoLab;
	}

//	public JCheckBox getMinInTrayCheckBut() {
//		return minInTrayCheckBut;
//	}
//
//	public JCheckBox getWinTopCheckBut() {
//		return winTopCheckBut;
//	}

	public JMenuItem getSelectAll() {
		return selectAll;
	}

	private void init(){
		this.addTaskBut=new JButton(ResourceMessages.getString("CommonComp.2"));
		this.addTaskBut.setActionCommand("addTask");
		this.addTaskBut.setToolTipText(ResourceMessages.getString("CommonComp.3"));
		
		this.delTaskBut=new JButton(ResourceMessages.getString("CommonComp.4"));
		this.delTaskBut.setActionCommand("delTask");
		this.delTaskBut.setToolTipText(ResourceMessages.getString("CommonComp.5"));
		
		this.addGoupBut=new JButton(ResourceMessages.getString("CommonComp.6"));
		this.addGoupBut.setActionCommand("addGoupBut");
		
		this.delGoupBut=new JButton(ResourceMessages.getString("CommonComp.7"));
		this.delGoupBut.setActionCommand("delGoupBut");
		
		this.ctrlBut=new JToggleButton(ResourceMessages.getString("CommonComp.8"));
		this.ctrlBut.setToolTipText(ResourceMessages.getString("CommonComp.9"));
		this.ctrlBut.setActionCommand("ctrl");
		this.clearBut=new JButton(ResourceMessages.getString("CommonComp.10"));
		this.clearBut.setActionCommand("clear");
		
		text=new JTextPane();
		text.setEditable(false);
		text.setFont(UIConst.FONT16);
		
		
		this.taskGroupList=new JList();
		DefaultListModel groupModel=new DefaultListModel();
		this.taskGroupList.setModel(groupModel);
		this.taskGroupList.setCellRenderer(new ListGroupLabel());
		this.taskGroupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		this.taskList=new JList();
		DefaultListModel listModel=new DefaultListModel();
		this.taskList.setModel(listModel);
		this.taskList.setCellRenderer(new ListLabel());
		
		DefaultTableModel tableModel=new DefaultTableModel(new String[]{ResourceMessages.getString("CommonComp.13"),ResourceMessages.getString("CommonComp.14")},0);
		infoTable=new JTable(tableModel){
			public boolean isCellEditable(int row, int column) {return false;};
		};
		infoTable.setColumnSelectionAllowed(false);
		infoTable.setRowSelectionAllowed(false);
		infoTable.getColumnModel().getColumn(0).setPreferredWidth(5);
		
		//状态栏
		this.startCountLab=new JTextField("0",2);
		this.startCountLab.setOpaque(false);
		this.startCountLab.setBorder(null);
		this.startCountLab.setEditable(false);
		this.stopCountLab=new JTextField("0",2);
		this.stopCountLab.setOpaque(false);
		this.stopCountLab.setBorder(null);
		this.stopCountLab.setEditable(false);
		this.totalCountLab=new JTextField("0",2);
		this.totalCountLab.setOpaque(false);
		this.totalCountLab.setBorder(null);
		this.totalCountLab.setEditable(false);
		
		
		//开始菜单项 
		this.logsMenu=new JMenuItem(ResourceMessages.getString("CommonComp.21"),new ImageIcon(this.getClass().getResource("images/logs.png")));
		this.modifyTaskMenu=new JMenuItem(ResourceMessages.getString("CommonComp.22"),new ImageIcon(this.getClass().getResource("images/modify.png")));
		this.saveMenu=new JMenuItem(ResourceMessages.getString("CommonComp.23"),new ImageIcon(this.getClass().getResource("images/save.png")));
		this.reloadMenu=new JMenuItem(ResourceMessages.getString("CommonComp.24"),new ImageIcon(this.getClass().getResource("images/reload.png")));
		this.minimiToTrayMenu=new JMenuItem(ResourceMessages.getString("Monitor.14"),new ImageIcon(this.getClass().getResource("images/minuTotray.png")));
		this.winTopMenu=new JMenuItem(ResourceMessages.getString("CommonComp.26"),new ImageIcon(this.getClass().getResource("images/wintop.png")));
		this.helpMenu=new JMenuItem(ResourceMessages.getString("CommonComp.27"),new ImageIcon(this.getClass().getResource("images/help.png")));
		this.aboutMenu=new JMenuItem(ResourceMessages.getString("CommonComp.28"),new ImageIcon(this.getClass().getResource("images/about.png")));
		this.exitMenu=new JMenuItem(ResourceMessages.getString("CommonComp.29"),new ImageIcon(this.getClass().getResource("images/exit-1.png")));
		this.nurseMenu=new JMenuItem(ResourceMessages.getString("CommonComp.33"),new ImageIcon(this.getClass().getResource("images/nurse.png")));
		
		//右键菜单
		this.taskPopupMenu=new JPopupMenu();
		this.newMenu=new JMenuItem(ResourceMessages.getString("CommonComp.3"),new ImageIcon(this.getClass().getResource("images/new.png")));
		this.modifyMenu=new JMenuItem(ResourceMessages.getString("CommonComp.22"),new ImageIcon(this.getClass().getResource("images/modify.png")));
		this.removeMenu=new JMenuItem(ResourceMessages.getString("CommonComp.5"),new ImageIcon(this.getClass().getResource("images/remove.png")));
		this.moveToMenu=new JMenu(ResourceMessages.getString("CommonComp.30"));
		this.selectAll=new JMenuItem(ResourceMessages.getString("CommonComp.35"),new ImageIcon(this.getClass().getResource("images/selelctall.png")));
		
//		minInTrayCheckBut=new JCheckBox(ResourceMessages.getString("CommonComp.31"));
//		winTopCheckBut=new JCheckBox(ResourceMessages.getString("CommonComp.32"));
		this.infoLab.setHorizontalAlignment(JLabel.CENTER);
		this.infoLab.setFont(UIConst.FONT16);
		this.infoLab.setForeground(Color.gray);
	}
	
	
	public JobTask[] getCurrentSelectItem(){
		Object[] oListValue=this.getTaskList().getSelectedValues();
		JobTask[]  jt=new JobTask[oListValue.length];
		for(int index=0;index<oListValue.length;index++){
			jt[index]=(JobTask)oListValue[index];
		}
		return jt;
	}
	
	public DefaultListModel getListModel(){
		return (DefaultListModel)this.getTaskList().getModel();
	}
	
	public KeyValue getCurrentSelectGroup(){
		return (KeyValue)this.taskGroupList.getSelectedValue();
	}
	
	public DefaultListModel getGroupModel(){
		return (DefaultListModel)this.taskGroupList.getModel();
	}
	
	public void changeTableProperty(boolean state){
		this.getInfoTable().setValueAt(state?ResourceMessages.getString("CommonComp.15"):ResourceMessages.getString("CommonComp.16").trim(), 3, 1);
	}
	
	public void displayInfo(){
		DefaultTableModel tableModel=(DefaultTableModel)this.getInfoTable().getModel();
		for(int count=tableModel.getRowCount()-1;count>=0;count--){
			tableModel.removeRow(count);
		}
		if(!this.taskList.isSelectionEmpty()){
			List<String[]> list=this.getCurrentSelectItem()[0].taskInfo();
			for(String[] s:list){
				tableModel.addRow(s);
			}
		}
	}
	
	public void startButtonStateChange(){
		if(this.ctrlBut.isSelected()){
			this.ctrlBut.setText(ResourceMessages.getString("CommonComp.16"));
			this.ctrlBut.setForeground(Color.RED);
		}else{
			this.ctrlBut.setText(ResourceMessages.getString("CommonComp.8"));
			this.ctrlBut.setForeground(Color.BLACK);
		}
	}
	
	public void addGroupToList(KeyValue group){
		this.getGroupModel().addElement(group);
		this.taskGroupList.setSelectedIndex(this.getGroupModel().getSize()-1);
	}
	
	public void addGroupsToList(KeyValue[] groups){
		for(int index=0;index<groups.length;index++){
			this.getGroupModel().addElement(groups[index]);
		}
		this.taskGroupList.setSelectedIndex(0);
	}
	
    public int addGroupItemsToList(List<JobTask> items){
    	this.getListModel().clear();
		for(Iterator<JobTask> iter=items.iterator();iter.hasNext();){
			this.getListModel().addElement(iter.next());
		}
		if(this.getListModel().getSize()>0){
			this.getTaskList().setSelectedIndex(0);
			this.displayInfo();
		}
		return this.getListModel().getSize();
	}
    
    public void addGroupItemToList(JobTask jt){
    	this.getListModel().addElement(jt);
    	this.getTaskList().setSelectedIndex(this.getListModel().getSize()-1);
    }
    
    public void removeGroupFromList(){
    	int index=this.taskGroupList.getSelectedIndex();
    	this.getGroupModel().removeElementAt(index);
    	this.taskGroupList.setSelectedIndex(index-1);
    }
    
    public void removeGroupItemFromList(){
    	int index=this.taskList.getSelectedIndex();
    	this.getListModel().removeElementAt(index);
    	if(!this.getListModel().isEmpty()){
    		int size=this.getListModel().getSize()-1;
    		this.taskList.setSelectedIndex(size<index?size:index);
    	}
    }
    
    public void palletized(ControlListener listener,boolean isEnable){
    	SystemTray sysTray=SystemTray.getSystemTray();
    	if(isEnable){
    		TrayIcon trayIcon=new TrayIcon(new ImageIcon(this.getClass().getResource("images/wintitle.png")).getImage(),ResourceMessages.getString("CommonComp.1"));
    		trayIcon.addMouseListener(listener);
    		trayIcon.setImageAutoSize(true);
    		try {
    			sysTray.add(trayIcon);
    		} catch (AWTException e) {
    			e.printStackTrace();
    		}
    	}else{
    			TrayIcon[] trayIcon=sysTray.getTrayIcons();
    			sysTray.remove(trayIcon[0]);
    	}
	}
    
    public void selectAll(int item){
    	int size=this.taskList.getModel().getSize();
    	ArrayList<Integer> itemIndexs=new ArrayList<Integer>();
    	JobTask firstJob=this.getSelectJobByIndex(item);
		for(int index=0;index<size;index++){
		    JobTask tempJob=this.getSelectJobByIndex(index);
			if(tempJob.getRunStatus()==firstJob.getRunStatus()){
				itemIndexs.add(index);
			}
		}
		int[] iCount=new int[itemIndexs.size()];
		for(int i=0;i<iCount.length;i++){
			iCount[i]=itemIndexs.get(i).intValue();
		}
		this.getTaskList().setSelectedIndices(iCount);
    }
    
    public void wasEnabled(boolean state){
    	this.modifyTaskMenu.setEnabled(state);
    	this.modifyMenu.setEnabled(state);
    }
    
    public JobTask getSelectJobByIndex(int index){
    	return (JobTask)this.getListModel().get(index);
    }
}
